﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO;
using System.Windows.Media.Imaging;
using bdnt_pets_system.SystemServiceReference;
using System.Windows.Data;
using bdnt_pets_system.Resources;
using bdnt_pets_system.Tools;
using bdnt_pets_system.FrameViews;
using System.Resources;
using bdnt_pets_system_Entity;
using System.Globalization;

namespace bdnt_pets_system.PageViews.SystemViews
{
    public partial class NewMenuControl : BaseUserControl
    {
        SystemServiceReference.SystemServiceClient sysClient;
        public NewMenuControl()
        {
            InitializeComponent();
        }
        public NewMenuControl(Tbl_Menu cuTbl_Menu)
        {
            InitializeComponent();

            if (cuTbl_Menu.Id == 0)
                tbPageIitle.SetBinding(TextBlock.TextProperty, new Binding("Strings.xjcd") { Source = CuLc });
            else
                tbPageIitle.SetBinding(TextBlock.TextProperty, new Binding("Strings.xgcd") { Source = CuLc });

            sysClient = new SystemServiceReference.SystemServiceClient();
            CuTbl_Menu = cuTbl_Menu;
            BingData();
            InitEvnet();
        }

        public Tbl_Menu CuTbl_Menu
        {
            get { return this.DataContext as Tbl_Menu; }
            set { this.DataContext = value; }
        }

        void BingData()
        {
            tbEMName.SetBinding(TextBox.TextProperty, new Binding("en_MenuName")
            {
                Mode = BindingMode.TwoWay,
                NotifyOnValidationError = true,
                ValidatesOnExceptions = true
            });
            tbZMName.SetBinding(TextBox.TextProperty, new Binding("MenuName")
            {
                Mode = BindingMode.TwoWay,
                NotifyOnValidationError = true,
                ValidatesOnExceptions = true
            });
            tbMModule.SetBinding(TextBox.TextProperty, new Binding("MenuModule") { Mode = BindingMode.TwoWay });
            tbMPId.SetBinding(TextBlock.TextProperty, new Binding("PMenuName") { Mode = BindingMode.TwoWay });
            tbMSq.SetBinding(TextBox.TextProperty, new Binding("MenuSequence")
            {
                Mode = BindingMode.TwoWay,
                NotifyOnValidationError = true,
                ValidatesOnExceptions = true
            });

            //加载菜单数据类型
            icLx.LoadDict(2, CuTbl_Menu.MenuType.ToString());

            if (CuTbl_Menu.Icon != null && CuTbl_Menu.Icon.Length > 0)
            {
                BitmapImage b = new BitmapImage();
                Stream mm = new MemoryStream();
                mm.Write(CuTbl_Menu.Icon, 0, CuTbl_Menu.Icon.Length);
                b.SetSource(mm);
                imgMenu.Source = b;
            }
        }

        void InitEvnet()
        {
            hbSelectImg.Click += new RoutedEventHandler(hbSelectImg_Click);
            btnOk.Click += new RoutedEventHandler(btnOk_Click);
            btnCancel.Click += new RoutedEventHandler(btnCancel_Click);
            sysClient.SaveMenuCompleted += new EventHandler<SaveMenuCompletedEventArgs>(sysClient_SaveMenuCompleted);
        }

        void sysClient_SaveMenuCompleted(object sender, SaveMenuCompletedEventArgs e)
        {
            if (e.Result)
                App.ToPage(new MenuManageControl()
                {
                    selectMId = CuTbl_Menu.Id > 0 ? CuTbl_Menu.Id : CuTbl_Menu.PMenuId,
                    isExpSelectM = CuTbl_Menu.Id > 0 ? false : true
                });
            else
                errorMsg.SetBinding(TextBlock.TextProperty, new Binding("Strings.bcsb") { Source = CuLc });
        }

        void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            App.ToPage(new MenuManageControl()
            {
                selectMId = CuTbl_Menu.Id > 0 ? CuTbl_Menu.Id : CuTbl_Menu.PMenuId,
                isExpSelectM = CuTbl_Menu.Id > 0 ? false : true
            });
        }
        /// <summary>
        /// 字段验证方法
        /// </summary>
        void BindgExpression()
        {
            tbEMName.GetBindingExpression(TextBox.TextProperty).UpdateSource();
            tbZMName.GetBindingExpression(TextBox.TextProperty).UpdateSource();
            tbMSq.GetBindingExpression(TextBox.TextProperty).UpdateSource();
        }

        void btnOk_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                BindgExpression();
                if (vs.Errors.Count > 0)
                {
                    errorMsg.Text = MyStrings.sjtxbq;// "数据填写不全！";
                    return;
                }

                var cuo = icLx.SelectedItem;
                if (cuo == null)
                {
                    errorMsg.Text = MyStrings.qxzcdlx;// "请选择菜单类型！";
                    return;
                }
                CuTbl_Menu.MenuType = Int32.Parse(cuo.Item1.DictValue);                 
                sysClient.SaveMenuAsync(CuTbl_Menu);
            }
            catch (Exception)
            {

            }
        }

        void hbSelectImg_Click(object sender, RoutedEventArgs e)
        {
            var open = new OpenFileDialog()
            {
                Filter =MyStrings.tp+ " (*.png)|*.png"
            };
            if (open.ShowDialog() == true)
            {
                FileStream stream = open.File.OpenRead();
                try
                {
                    byte[] bs = new byte[stream.Length];
                    BitmapImage bi = new BitmapImage();
                    stream.Read(bs, 0, bs.Length);
                    CuTbl_Menu.Icon = bs;
                    bi.SetSource(stream);
                    imgMenu.Source = bi;                   
                }
                catch (Exception ex)
                {
                    MessageBox.Show(MyStrings.tpxzyyc);
                }
                finally
                {
                    stream.Close();
                    stream.Dispose();
                }
            }
        }

    }
}
